package com.yanyan.sannuomin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyan.sannuomin.result.Result;
import com.yanyan.sannuomin.result.ResultCode;
import com.yanyan.sannuomin.service.MenuService;
import com.yanyan.sannuomin.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单信息")
public class MenuController{
    @Resource
    private MenuService menuService;

    @RequestMapping(value = "findMenu",method = RequestMethod.GET)
    @ApiOperation(value = "查询菜单",httpMethod = "GET")
    public Result findMenu(Integer page,Integer limit) throws Exception {
        ObjectMapper objectMapper=new ObjectMapper();
        Page page1= PageHelper.startPage(page,limit);
        List<MenuVo> menuVos=menuService.findMenu();
        PageInfo pageInfo=new PageInfo(menuVos);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),menuVos);


    }

    @RequestMapping(value = "{menuId}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除菜单",notes = "删除菜单")
    public Result updateMenu(@PathVariable("menuId") Integer menuId) throws Exception{
        int i=menuService.delete(menuId);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.ok(ResultCode.FAIL,null);
        }
    }

}